var app = getApp()
var $ = require("../../utils/httpHelper.js")
var timeout = false; //启动及关闭按钮 
var h = 0
Page({
  data: {
    commonlist:[],
    common:'',
    last_id:0,
    scrollTop:0,
    room_id:0,
    class_id:0
  },
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        h = res.windowHeight
        that.setData({ 
          height: res.windowHeight 
        })
      }
    })

    if(options.class_id){
      this.setData({
        class_id:options.class_id,
        room_id:wx.getStorageSync('room_id')
      })
    }
  },
  onShow(){
    timeout = false
    this.time()
  },
  time(){
    if (timeout) return;
    this.getData()
    setTimeout(this.time, 1000);
  },
  onUnload(){
    timeout = true
  },
  getData(){
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
      last_id: this.data.last_id,
      room_id:this.data.room_id,
      class_id:this.data.class_id
    }
    $.httpPost("chat_list", params, (data, err) => {
      if (data.data.list.length == 0) {
        return
      }
      let arr = this.data.commonlist
      for (let item of data.data.list) {
        arr.push(item)
      }
      this.setData({
        commonlist: arr,
        last_id: data.data.list[data.data.list.length - 1].id
      })
      this.setData({
        scrollTop: this.data.scrollTop + h
      })
    })
  },
  commoninput(e){
    this.setData({
      common: e.detail.value
    })
  },
  submit(){
    let com = this.data.common
    if(app.check_empty(com)){
      wx.showToast({
        title: '不能是空哦',
      })
      return
    }
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
      content:com,
      room_id: this.data.room_id,
      class_id: this.data.class_id
    }
    $.httpPost("add_chat", params, (data, err) => {
      if(data.result == 'success'){
        this.setData({
          common:''
        })
      }
    })
    
  }
  

  
})
